﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Solution.Business.Entities;
using Solution.Business.Component;
using Solution.Data;
using MvcPaging;
using System.Globalization;

namespace Solution.UI.Web.Controllers
{
    public class FreeNewsController : Controller
    {
        private Solution.Business.Entities.SolutionEntities db = new SolutionEntities();
        private const int DefaultPageSize = 50;
        private IList<FreeNew> list;
        //
        // GET: /FreeNews/

        public ActionResult Create()
        {
            var list = Business.Component.LegalComponent.Legal_GetByTop("500", "", "");
            ViewBag.LgId = new SelectList(list, "LgId", "Name", "");
            var list2 = Business.Component.TransactionComponent.Transaction_GetByTop("500", "len([Level]) = 4", "");
            ViewBag.TrId = new SelectList(list2, "TrId", "Name", "");     
                return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(FreeNew model)
        {
            if (ModelState.IsValid)
            {
                if (model.Title != null && model.Details != null && model.LgId != null && model.TrId != null)
                {
                    model.Date = DateTime.Now;
                    model.UId = Int32.Parse(Session["UserId"].ToString());
                    Business.Component.FreeNewsComponent.FreeNews_Insert(model);
                    Business.Component.FreeNewsComponent.FreeNews_Update(model);
                    return RedirectToAction("Index", "Home");
                }

                else
                {
                    ModelState.AddModelError("", "Bạn chưa điền đầy đủ thông tin cần nhập!");
                }

            }

            var list = Business.Component.LegalComponent.Legal_GetByTop("500", "", "");
            ViewBag.LgId = new SelectList(list, "LgId", "Name", model.LgId);
            var list2 = Business.Component.TransactionComponent.Transaction_GetByTop("500", "len([Level]) = 4", "");
            ViewBag.TrId = new SelectList(list2, "TrId", "Name", model.TrId);
            return View(model);
        }

        public ActionResult ShowFreeNews1()
        {
            var list = Business.Component.FreeNewsComponent.FreeNews_GetByTop("15", "TrId in (select TrId from [Transaction] where TrGroup =1)", "Date desc");
            return PartialView(list);
        }

        public ActionResult ShowAllFreeNews1(int? page, FormCollection form, String submitButton)
        {
            if (submitButton == "Go")
            {
                string txtTitle = form.GetValues("txtTitle") != null ? form.GetValues("txtTitle").First() : "";
                string date = form.GetValues("datepicker").First();
                if (date != "")
                {
                    list = Business.Component.FreeNewsComponent.FreeNews_GetByTop("1000", "Title like '%" + txtTitle + "%' and Date = left('" + date + "', 10) and TrId in (select TrId from [Transaction] where TrGroup =1)", "");
                }
                else
                {
                    list = Business.Component.FreeNewsComponent.FreeNews_GetByTop("1000", "Title like '%" + txtTitle + "%' and TrId in (select TrId from [Transaction] where TrGroup =1)", "Date desc");
                }
            }
            else
            {
                list = Business.Component.FreeNewsComponent.FreeNews_GetByTop("1000", "TrId in (select TrId from [Transaction] where TrGroup =1)", "Date desc");
            }
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            return View(this.list.ToPagedList(currentPageIndex, DefaultPageSize));
        }

        
        public ActionResult Details(int FNId)
        {
            var list = Business.Component.FreeNewsComponent.FreeNews_GetByTop("1", "FNId = '" + FNId + "'", "");
            Session["UserId"] = list[0].UId;
            return View(list);
        }

        public ActionResult DetailsRelate()
        {
            int UId = Int32.Parse(Session["UserId"].ToString());
            var list = Business.Component.FreeNewsComponent.FreeNews_GetByTop("15", "UId = '"+ UId +"'", "");
            return PartialView(list);
        }

        public ActionResult DetailsNew()
        {
            var list = Business.Component.FreeNewsComponent.FreeNews_GetByTop("15", "", "Date desc");
            return PartialView(list);
        }

        public ActionResult ShowFreeNews2()
        {
            var list = Business.Component.FreeNewsComponent.FreeNews_GetByTop("15", "TrId in (select TrId from [Transaction] where TrGroup =2)", "Date desc");
            return PartialView(list);
        }

        public ActionResult ShowAllFreeNews2(int? page, FormCollection form, String submitButton)
        {
             if (submitButton == "Tìm Kiếm")
            {
                string txtTitle = form.GetValues("txtTitle") != null ? form.GetValues("txtTitle").First() : "";
                string date = form.GetValues("datepicker").First();
                if (date != "")
                {
                    list = Business.Component.FreeNewsComponent.FreeNews_GetByTop("1000", "Title like '%" + txtTitle + "%' and Date = left('" + date + "', 10) and TrId in (select TrId from [Transaction] where TrGroup =2)", "");
                }
                else
                {
                    list = Business.Component.FreeNewsComponent.FreeNews_GetByTop("1000", "Title like '%" + txtTitle + "%' and TrId in (select TrId from [Transaction] where TrGroup =2)", "Date desc");
                }
            }
            else
            {
                list = Business.Component.FreeNewsComponent.FreeNews_GetByTop("1000", "TrId in (select TrId from [Transaction] where TrGroup =2)", "Date desc");
            }
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            return View(this.list.ToPagedList(currentPageIndex, DefaultPageSize));
        }

    }
}
